package cn.topcodes.tcsf.wechat.service;

import cn.topcodes.tcsf.admin.commons.generic.GenericService;
import cn.topcodes.tcsf.wechat.domain.entity.WechatUserBranch;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import tk.mybatis.mapper.entity.Example;
import java.util.List;

@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class WechatUserBranchServiceImpl extends GenericService<WechatUserBranch> implements WechatUserBranchService {

    @Override
    public WechatUserBranch findByOpenId(String openId) {
        Assert.notNull(openId, "openId不能为空");
        Example example = this.buildExample();
        example.or().andEqualTo(WechatUserBranch.FIELD_OPEN_ID, openId);
        return this.findOne(example);
    }

    @Override
    public List<WechatUserBranch> findAllByUserId(Long userId) {
        Assert.notNull(userId, "userId不能为空");
        Example example = this.buildExample();
        example.or().andEqualTo(WechatUserBranch.FIELD_USER_ID, userId);
        return this.findAll(example);
    }

    @Override
    public WechatUserBranch findBy(Long wechatMpId, String openId) {
        Assert.notNull(wechatMpId, "wechatMpId不能为空");
        Assert.notNull(openId, "openId不能为空");
        Example example = this.buildExample();
        example.or().andEqualTo(WechatUserBranch.FIELD_OPEN_ID, openId)
                .andEqualTo(WechatUserBranch.FIELD_TYPE, WechatUserBranch.TYPE_MP)
                .andEqualTo(WechatUserBranch.FIELD_WECHAT_MP_ID);
        return this.findOne(example);
    }

}
